package com.shadow.disnet.netty.client;

import com.shadow.disnet.netty.ILog;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.util.ReferenceCountUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sun.nio.cs.ext.MS874;

public class MyClientInBoundHandler extends ChannelInboundHandlerAdapter implements ILog {
    private Channel channel;

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        info("入站通道激活");
        ctx.channel().writeAndFlush("Hello World\r\n"); //客户端发送消息
        this.channel= ctx.channel();
    }

    @Override
    public void channelRegistered(ChannelHandlerContext ctx) throws Exception {
        info("入站通道注册");
    }

    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {

        info("入站通道关闭");
    }

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        try {
            info("客户端入站处理器读取到消息：%s", msg);
        }finally {
            ReferenceCountUtil.release(msg);
        }

    }

    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
        info("入站处理器读取消息完毕");
    }

    @Override
    public Logger getLogger() {
        return LoggerFactory.getLogger(this.getClass());
    }

    @Override
    public String toString() {
        return "入站处理器";
    }
}